package com.zhou.imaotai.utils;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.zhou.imaotai.entity.IUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lcz
 * @desc 推送实用程序
 * @mail lcz_0130@163.com
 * @date 2021/3/22 16:52
 */

@Service
@Slf4j
public class PushUtil {
    @Autowired
    private JavaMailSender mailSender;
    // 发送邮件的邮箱
    @Value("${spring.mail.username}")
    private String from;

    // 阿里云短信服务 AccessKey ID
    @Value("${sms.aliyun.accessKeyId}")
    private String accessKeyId;
    // 阿里云短信服务 AccessKey Secret
    @Value("${sms.aliyun.accessKeySecret}")
    private String accessKeySecret;
    // 阿里云短信服务 签名名称
    @Value("${sms.aliyun.signName}")
    private String signName;
    // 你申请的短信模板CODE
    @Value("${sms.aliyun.templateCode}")
    private String templateCode;

    /**
     * 发送信息
     */
    public void sendMsg(IUser iUser, String subject, String content) {
        sendSimpleMail(iUser.getPushEmail(), subject, content);
        //阿里云短信服务
        // 短信模板中的变量值
        JSONObject templateJson = new JSONObject();
        //设置时间格式 年-月-日 时:分:秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        templateJson.put("time", sdf.format(new Date()));
        templateJson.put("user", "州仔");
        templateJson.put("name", iUser.getRemark());
        templateJson.put("psw", subject + content);
        sendSms(iUser, templateJson.toJSONString());
    }

    /**
     * 简单文本邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        mailSender.send(message);
    }


    /**
     * 发送短信
     */
    public void sendSms(IUser iUser, String templateParam) {

        // 设置你的AccessKey ID和AccessKey Secret
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        // 构造请求
        SendSmsRequest request = new SendSmsRequest();
        //手机号
        request.setPhoneNumbers(iUser.getMobile().toString());
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam(templateParam);


        try {
            // 发送请求
            SendSmsResponse response = client.getAcsResponse(request);
            log.info("RequestId=" + response.getRequestId() + ", Code=" + response.getCode() + ", Message=" + response.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

